package com.ttb.privatebox;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.ttb.bubbles.HelloBubblesActivity;
import com.ttb.database.Contact;
import com.ttb.database.ContactDAO;
import com.ttb.smshelper.R;
import com.ttb.smshelper.list_private;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class CustomizedListView extends Activity {
	public static final String KEY_SONG = "song"; // parent node
	public static final String KEY_ID = "id";
	public static final String KEY_TITLE = "title";
	public static final String KEY_ARTIST = "artist";
	public static final String KEY_DURATION = "duration";
	public static final String KEY_THUMB_URL = "thumb_url";
	
	ListView list;
	LazyAdapter adapter;
	EditText editName, editPhone;
	Button btnAdd;
	ArrayList<list_private> arrPrivate = new ArrayList<list_private>();
	ArrayList<Contact> contactList = new ArrayList<Contact>();
	ArrayAdapter<Contact> contactAdapter = null;
	ListView lvprivatelist;
	ContactDAO contactDAO;
	ArrayList<HashMap<String, String>> contactsList;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.manhinh_privatebox);
		contactDAO = new ContactDAO(this.getApplicationContext());
		doFormWidgets();
		addEventFormWidgets();
		loadTabs();
		getDefaultInfor();
		contactsList = new ArrayList<HashMap<String, String>>();
		ContactDAO contactDAO = new ContactDAO(this.getApplicationContext());
		List<Contact> listContact = contactDAO.getAllContacts();
		// looping through all song nodes <song>
		Iterator<Contact> iterator = listContact.iterator();
		while (iterator.hasNext()) {
			Contact contact = iterator.next();
			HashMap<String, String> map = new HashMap<String, String>();
			// adding each child node to HashMap key => value
			map.put(KEY_ID, String.valueOf(contact.getId()));
			String name = contact.getName();;
			if(contact.getName() == null){
				name = contact.getPhone_number();
			}
			map.put(KEY_TITLE, name);
			map.put(KEY_ARTIST, contact.getPhone_number());
			map.put(KEY_DURATION, "10:34");
			map.put(KEY_THUMB_URL, "http://api.androidhive.info/music/images/adele.png");
			// adding HashList to ArrayList
			contactsList.add(map);
		}

		list = (ListView) findViewById(R.id.list);

		// Getting adapter by passing xml data ArrayList
		adapter = new LazyAdapter(this, contactsList);
		list.setAdapter(adapter);

		// Click event for single list row
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(CustomizedListView.this,
						HelloBubblesActivity.class);
				String contact_id = contactsList.get(position).get(KEY_ID);
				intent.putExtra(KEY_ID, contact_id);
				startActivity(intent);
			}
		});
	}

	private void loadTabs() {
		final TabHost tab = (TabHost) findViewById(android.R.id.tabhost);

		tab.setup();
		TabHost.TabSpec spec;

		spec = tab.newTabSpec("t1");
		spec.setContent(R.id.tab1_private);
		spec.setIndicator("Private Message");
		tab.addTab(spec);
		spec = tab.newTabSpec("t2");
		spec.setContent(R.id.tab2_private);
		spec.setIndicator("New");
		tab.addTab(spec);

		tab.setCurrentTab(0);

		tab.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			public void onTabChanged(String arg0) {
				String s = "Tab tag =" + arg0 + "; index ="
						+ tab.getCurrentTab();
			}
		});

	}
	private void getDefaultInfor() {
		editName.requestFocus();
		List<Contact> contacts = contactDAO.getAllContacts();
		for(Contact contact : contacts)
		{
			contactList.add(contact);
		}
		contactAdapter.notifyDataSetChanged();
	}

	private void doFormWidgets() {

		editName = (EditText) findViewById(R.id.editName);
		editPhone = (EditText) findViewById(R.id.editPhone);
		btnAdd = (Button) findViewById(R.id.btnAddContact);
		lvprivatelist = (ListView) findViewById(R.id.lvprivatelist);
		contactAdapter = new ArrayAdapter<Contact>(this,
				android.R.layout.simple_list_item_1, contactList);
		lvprivatelist.setAdapter(contactAdapter);

	}

	private void addEventFormWidgets() {

		btnAdd.setOnClickListener(new MyButtonEvent());
		lvprivatelist.setOnItemLongClickListener(new MyListViewEvent());

	}

	private class MyButtonEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {

			case R.id.btnAddContact:
				processAddJob();
				break;
			}
		}

		private void processAddJob() {

			Log.d("click","BInh");
			String name = editName.getText() + "";
			String phone = editPhone.getText() + "";
			Contact contact = new Contact(name,phone);
			//add to arraylist
			contactList.add(contact);
			//add to db
			contactDAO.addContact(contact);
			contactAdapter.notifyDataSetChanged();
			
			editName.setText("");
			editPhone.setText("");
			editName.requestFocus();
		}

	}

	private class MyListViewEvent implements OnItemLongClickListener {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
				int arg2, long arg3) {
			Contact contact = contactList.get(arg2);
			contactList.remove(contact);
			contactDAO.deleteContact(contact.getId());
			contactAdapter.notifyDataSetChanged();
			return false;
		}

	}

}